    //
//  MenuUMLDiagram.m
//  ModuleTesting
//
//  Created by duc nguyen van on 8/25/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "MenuUMLDiagram.h"
#import "Shape.h"

#define IMAGE_WIDTH_PROJECTS	688	
#define IMAGE_HEIGHT_PROJECTS	532

#define IMAGE_WIDTH_UML			200
#define IMAGE_HEIGHT_UML		200

@implementation MenuUMLDiagram
@synthesize scrollViewProjectChild,scrollViewUmlChild,pageControlProjects,textTitle,labelDate;

#pragma mark -
#pragma mark save and load file

-(NSString*)pathForDataFileWithName:(NSString*)name{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentDictonary = [paths objectAtIndex:0];
	
	NSString *nameFile = [NSString stringWithFormat:@"/%@",name];
	NSString *filePath = [documentDictonary stringByAppendingFormat:nameFile];
	return filePath;
}

-(void)saveDataToDisk{
	NSMutableDictionary *rootSave = [NSMutableDictionary dictionary];
	[rootSave setValue:projects forKey:@"projects"];
	[NSKeyedArchiver archiveRootObject:rootSave toFile:[self pathForDataFileWithName:@"umlDiagramSave"]];
}

-(void)loadDataFromDisk{
	NSDictionary *rootLoad = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForDataFileWithName:@"umlDiagramSave"]];
	projects = [rootLoad valueForKey:@"projects"];
}

#pragma mark -
#pragma mark textTitle delegate

-(NSString*)getNowDateFormat{
	NSDate *tempDate = [NSDate date];
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	[formatter setDateFormat:@"dd/MM/yyyy"];
	
	return [formatter stringFromDate:tempDate];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
	[self loadDataFromDisk];
	
	int page = pageControlProjects.currentPage;
	Project *temp = [projects objectAtIndex:page];
	temp.date = [self getNowDateFormat];
	temp.name = textField.text;
	
	[self saveDataToDisk];
}

#pragma mark -
#pragma mark scrollDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	[self loadDataFromDisk];
	
	CGFloat pageWidth = scrollViewProjectChild.frame.size.width;
	int page = floor((scrollViewProjectChild.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
	pageControlProjects.currentPage = page;
	
	Project *tempPro = [projects objectAtIndex:page];
	textTitle.text = tempPro.name;
	labelDate.text = tempPro.date;
	
	NSLog(@"did scroll  count is %d and page is %d",[projects count],page);
}

#pragma mark -
#pragma mark Load ScrollView

-(void)setResourceOfProjectScrollView{
	int i = 0;
	for (Project *pro in projects) {
		if (i == 0) {
		 	textTitle.text = pro.name;
			labelDate.text = pro.date;
		}
		
		if ([pro.umlDiagrams count] > 0) {
			UIImage *image = [UIImage imageNamed:@"test.jpg"];
			UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
			
			[imageView.layer setBorderWidth:5.0];
			
			imageView.contentMode = UIViewContentModeScaleToFill;
			//imageView.clipsToBounds = YES;
			
			imageView.frame = CGRectMake( IMAGE_WIDTH_PROJECTS * i, 0, IMAGE_WIDTH_PROJECTS, IMAGE_HEIGHT_PROJECTS);
			[scrollViewProjectChild addSubview:imageView];
			[imageView release];
		}
		else {
			UIImage *image = [UIImage imageNamed:@"newProject.jpg"];
			UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
			
			[imageView.layer setBorderWidth:5.0];
			
			imageView.contentMode = UIViewContentModeScaleToFill;
			//imageView.clipsToBounds = YES;
			
			imageView.frame = CGRectMake( IMAGE_WIDTH_PROJECTS * i, 0, IMAGE_WIDTH_PROJECTS, IMAGE_HEIGHT_PROJECTS);
			[scrollViewProjectChild addSubview:imageView];
			[imageView release];
		}
		i++;
	}
	scrollViewProjectChild.contentSize = CGSizeMake(IMAGE_WIDTH_PROJECTS * i, IMAGE_HEIGHT_PROJECTS);
}

#pragma mark -
#pragma mark handle event

//tapgesture
-(void)tapScrollView:(UIGestureRecognizer*)tap{
	NSLog(@"has double tapped");
}

-(CGRect)rectForScrollViewProjectWithIndex:(int)index{
	CGRect rect = scrollViewProjectChild.frame;
	rect.origin.x = rect.size.width * index;
	rect.origin.y = 0; 
	return rect;
}

-(IBAction)addProject:(id)sender{
	[self loadDataFromDisk];
	
	int page = pageControlProjects.currentPage;
	
	Project *newProject = [[Project alloc] init];
	if (projects == nil) {
		projects = [[NSMutableArray alloc] init];
	}
	
	[projects addObject:newProject];
	
	pageControlProjects.numberOfPages = [projects count];
	pageControlProjects.currentPage = [projects count];
	
	[self setResourceOfProjectScrollView];
	
	[scrollViewProjectChild scrollRectToVisible:[self rectForScrollViewProjectWithIndex:([projects count] - 1)] 
									   animated:YES];
	NSLog(@"add count is %d and page is %d",[projects count],page);
	[self saveDataToDisk];
	[newProject release];
}

-(IBAction)deleteProject:(id)sender{
	[self loadDataFromDisk];
	if ([projects count] > 0){
		
		int page = pageControlProjects.currentPage;
		[projects removeObjectAtIndex:page];
		[scrollViewProjectChild scrollRectToVisible:[self rectForScrollViewProjectWithIndex:page - 1] 
										   animated:YES];
		
		pageControlProjects.numberOfPages = [projects count];
		pageControlProjects.currentPage = page - 1;
		
		
		NSLog(@"delete count is %d and page is %d",[projects count],page);
		[self setResourceOfProjectScrollView];
		[self saveDataToDisk];	
	}
}

-(IBAction)duplicateProject:(id)sender{
	
}

#pragma mark -
#pragma mark initializer
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	projects = [[NSMutableArray alloc] init];
	
	[self loadDataFromDisk];
	[self setResourceOfProjectScrollView];
	
	pageControlProjects.numberOfPages = [projects count];
	pageControlProjects.currentPage = 0;
	
	scrollViewProjectChild.delegate = self;
	scrollViewUmlChild.delegate = self;
	textTitle.delegate = self;
	
	UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScrollView:)];
	tap.numberOfTapsRequired = 2;
	tap.numberOfTouchesRequired = 1;
	[scrollViewProjectChild addGestureRecognizer:tap];
	[tap release];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[projects release];
    [super dealloc];
}


@end
